//
//  ViewController.m
//
//  Created by Scott on 16/1/20.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"
#import "TouchView.h"
#import "NetworkingHandler.h"

/** 函数声明 */
int sum(int, int);

/** 函数实现. */
int sum(int x, int y) {
    
    return x + y;
}

int sub(int x, int y) {
    return x - y;
}

@interface ViewController ()

@property (nonatomic, copy) void (^block)(NSString *);

@property (nonatomic, retain) NSString *name;


@property (weak, nonatomic) IBOutlet UILabel *label;

@property (weak, nonatomic) IBOutlet TouchView *touchView;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self functionReview];
    [self studyBlock];
    
    
    [self.touchView viewDidClick:^(TouchView *view) {
        
        NSLog(@"点击我了.");
        
    }];
    
    
    [self.touchView buttonHandle:^(UIButton *deleBtn) {
        
        NSLog(@"点击了删除按键.");
    }];
    
    
    [self.touchView editBtnHandle:^(UIButton *editBtn) {
        NSLog(@"点击了编辑按键");
        
        
    } deleBtnHandle:^(UIButton *deleBtn) {
        
        NSLog(@"点击了删除按键");
        
    }];
    
    [self handleData];

}

#pragma mark - 数据处理(网络类使用Block回调) 
- (void)handleData {
    
    NSString *str = @"http://api.map.baidu.com/place/v2/search?query=银行&region=大连&output=json&ak=6E823f587c95f0148c19993539b99295";
    
    [NetworkingHandler networkingWithURL:str completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        NSLog(@"%@", result);
        
    }];
    
}



#pragma mark - 函数复习
- (void)functionReview {
    /** 
     * 函数由三部分: 函数的声明, 函数的实现, 函数的调用.
     */
    
    NSLog(@"%d", sum(3, 5));
    
    /** 
     * 函数指针: 指针指向一块内存. 里面是一段代码段.
     *
     */
    
    /** 定义一个函数指针变量. */
    int (*p)(int, int) = NULL;
    
    // 函数指针变量赋值.
    p = sum;
    p = sub;
    
    // 通过函数指针调用函数.
    p(3, 5);
    NSLog(@"%d", p(3, 5));

}

#pragma mark - block 
- (void)studyBlock {
    
    /** 
     * 1. Block 和函数指针很相似. 技巧: *p 换成 ^block
     
     * 2. Block 相当于匿名函数, 所有blockbi
     *
     * 所以, block变量赋值时,直接赋函数实现部分.
     */
    
    int (^block1)(int, int) = ^int (int x, int y) {
        
        return x + y;
    };
    

    // 调用block
    block1(3, 5);
    NSLog(@"%d", block1(3, 5));
    
    
    // block 语法.
    // 无参无返block
    void (^block2)(void) = ^(void) {
        
        NSLog(@"无参无返.");
    };
    block2();
    
    // 无参有返block
    NSString * (^block3)(void) = ^NSString *(void) {
        
        return @"无参有返";
    };
    block3();
    NSLog(@"%@", block3());
    
    // 有参无返
    void (^block4)(NSString *, NSString *) = ^(NSString *str1, NSString *str2) {
        
        NSLog(@"%@", [str1 stringByAppendingString:str2]);

    };
    block4(@"da", @"lian");
    
    // 有参有返
    NSString * (^block5)(NSString *, NSString *) = ^NSString * (NSString *str1, NSString *str2) {
        
        return [str1 stringByAppendingString:str2];
        
    };
    
    NSLog(@"%@", block5(@"hello", @"world!"));
    
    
    
    void (^block)(NSString *str) = ^(NSString *str) {
      
        NSLog(@"%@", str);
    };
    
    
    block(@"dd");
    

    self.block = ^(NSString *str) {
        
        NSLog(@"%@", str);
        
    };

}


- (IBAction)handleBlock:(id)sender {
    // 调用block
    self.block(@"aaa");
    
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    // 获取第二界面的对象.
    SecondViewController *second = [segue destinationViewController];
    
    second.block = ^(NSString *str) {
        
        self.label.text = str;
        
    };
}



#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
